from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def index(request):
    # return HttpResponse('ahah')   #地址正确
    return render(request,'myadmin/index.html')

def uploads(myfile):
    import time,random
    from web.settings import BASE_DIR
    filename = str(time.time()) + str(random.randrange(1,9999999999999,1)) + '.' + myfile.name.split('.').pop()
    # 文件写入数据库
    pic_bin = open(BASE_DIR + '/static/pics/' + filename,'wb+')
    # 分块写入
    for chunk in myfile.chunks():
        pic_bin.write(chunk)
    pic_bin.close()
    return '/static/pics/' + filename

# ===================================================
# def uploads(myfile):
#     '''
#     myfile  文件对象
#
#     '''
#     import time,random
#     from web.settings import BASE_DIR
#     filename = str(time.time()) + str(random.randrange(1,99999999999999,1)) + '.' + myfile.name.split('.').pop()
#     pic_bin = open(BASE_DIR + '/static/pics/' + filename,'wb+')
#     # 分块写入
#     for chunk in myfile.chunks():
#         pic_bin.write(chunk)
#         return '/static/pics/' + filename



# 登录
def login(request):
    if request.method == 'GET':

        # 返回一个登录页面
        return render(request, 'myadmin/login.html')

    elif request.method == 'POST':
        # 执行登录

        if request.POST['username'] == 'admin' and request.POST['password'] == '123456':
            # 执行登陆
            request.session['AdminUser'] = {'username': 'admin', 'uid': 10}
            return HttpResponse('<script>alert("登录成功");location.href="/myadmin/"</script>')

    return HttpResponse('<script>alert("用户名或密码错误");location.href="/myadmin/login/"</script>')


# 退出
def logout(request):
    request.session['AdminUser'] = None

    return HttpResponse('<script>alert("欢迎下次登录");location.href="/myadmin/login/"</script>')

























